package com.aries.crossgate.modules.sys.service.imp;

import com.aries.crossgate.common.utils.Constant;
import com.aries.crossgate.common.utils.R;
import com.aries.crossgate.modules.sys.entity.SysMenu;
import com.aries.crossgate.modules.sys.dao.SysMenuDao;
import com.aries.crossgate.modules.sys.service.SysMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService{


    @Autowired
    private SysMenuDao sysMenuDao;

    /**
     * @description: 保存用户
     * @author 李贝留斯
     * @email sunyadongsyd@163.com
     * @date 2018/5/10 22:45
     */
    public R save(SysMenu sysMenu) {
        //SysMenu.setMenuId(UUID.randomUUID().toString().replace("-",""));
        int row = sysMenuDao.insert(sysMenu);
        if(row>0){
            return R.ok(Constant.SUCCESS);
        }else {
            return R.error(Constant.ERROR);
        }
    }

    @Override
    public R delete(String ids) {
        String[] id = ids.split(",");
        for(int i=0;i<id.length;i++){
            sysMenuDao.deleteById(id[i]);
        }
        return R.ok(Constant.SUCCESS);
    }

    @Override
    public R update(SysMenu SysMenu) {
        int row = sysMenuDao.updateById(SysMenu);
        if(row>0){
            return R.ok(Constant.SUCCESS);
        }else {
            return R.error(Constant.ERROR);
        }
    }

    public List<SysMenu> getOneTwoMenu() {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("type","1").or("type","2");
        return sysMenuDao.selectList(entityWrapper);
    }

    public List<SysMenu> getSysMenu() {
        EntityWrapper entityWrapper = new EntityWrapper();
        return sysMenuDao.selectList(entityWrapper);
    }
    @Override
    public List<SysMenu> getSysMenu(String name,String menuId) {
        EntityWrapper entityWrapper = new EntityWrapper();
        if(StringUtils.isNotEmpty(name))
            entityWrapper.eq("name",name);
        if(StringUtils.isNotEmpty(menuId))
            entityWrapper.eq("menu_id",menuId);
        return sysMenuDao.selectList(entityWrapper);
    }

    public SysMenu getMenuById(String id) {
        SysMenu SysRole = sysMenuDao.selectById(id);
        return SysRole;
    }

    public List<SysMenu> listMenuTree(String userId) {
        List<SysMenu> trees = new ArrayList<SysMenu>();
        List<SysMenu> listMenu = sysMenuDao.listMenuByUserId(userId);
        return listMenu;
    }

    @Override
    public Set<String> listPerms(String userId) {
        List<String> perms = baseMapper.listUserPerms(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (org.apache.commons.lang3.StringUtils.isNotBlank(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }
}
